<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Dynamic Text Resizing with ResizeObserver</title>
    <style>
        #textContainer {
            width: 300px;
            height: 200px;
            background-color: lightyellow;
            overflow: hidden;
            transition: width 0.3s, height 0.3s;
        }
        #text {
            font-size: 16px;
            transition: font-size 0.3s;
        }
    </style>
</head>
<body>
<div id="app">
    <div id="textContainer">
        <div id="text">
            This is a sample text that will dynamically adjust its size based on the container's dimensions.
        </div>
    </div>
</div>

<script>
    document.addEventListener('DOMContentLoaded', () => {
        const textContainer = document.getElementById('textContainer');
        const text = document.getElementById('text');

        const resizeObserver = new ResizeObserver(entries => {
            entries.forEach(entry => {
                console.log(entry);
                const { width, height } = entry.contentRect;
                const fontSize = Math.min(width / 20, height / 10);
                text.style.fontSize = `${fontSize}px`;
            });
        });

        resizeObserver.observe(textContainer);

        // 模拟容器尺寸变化
        setTimeout(() => {
            textContainer.style.width = '400px';
            textContainer.style.height = '300px';
        }, 2000);
    });
</script>
</body>
</html>

